"""count words by files"""

import os;

PATH = "E:\spark\python\python\mllib"
PATH_FS = os.path.abspath(PATH)
dct = {}


def count_words(line):
    line = line.strip()
    words = line.split(" ")
    for x in words:
        if (x not in dct):
            dct[x] = 1
        else:
            dct[x] = dct[x] + 1

    return len(words)


def word_count1(x):
    cnt = 0
    fp = open(x, "r")
    # print (fp.readlines())
    for line in fp.readlines():
        # print line
        cnt += count_words(line)

    return cnt


def go():
    fs = os.listdir(PATH)
    print str(len(fs)) + " files found"
    cnt = 0
    cc = 0
    for x in fs:
        fs = PATH_FS + "\\" + x
        # print fs
        cc += word_count1(fs)
        cnt += cc
        print("total word of %s is: %d" % (fs, cc))
        cc = 0

    return cnt


if __name__ == '__main__':
    total = go()
    # sort
    [v for v in sorted(dct.values())]
    print("total word is:" + str(total))
    for x in dct:
        print x, dct[x]
